package samples;


import java.awt.Point;

import org.jbox2d.collision.shapes.CircleShape;
import org.jbox2d.collision.shapes.PolygonShape;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.BodyType;
import org.jbox2d.dynamics.FixtureDef;
import org.jbox2d.dynamics.World;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Rectangle;


public class Collision extends BasicGame{

	public Collision(String title) {
		super(title);
	}

	//atributos
	World world;
	BodyDef bDSueloInidial, bDSueloFInal, bDPlayer;
	BodyDef groundDef;
	Rectangle player, sueloInicialDibujado, sueloFinalDibujo, techo;
	final float WIDTH = 10;
	final float HEIGHT = 25;
	Body bPlayer, bGroundInicial, bGroundFinal;
	private Body ground;
	BodyDef groundBodyDef, secondGroundBodyDef, roofBodyDef;
	PolygonShape groundBox, dynamicBox, secondGroundBox;
	FixtureDef fixtureDef;
	float timeStep = 1.0f/60.0f;
	int velocityIterations = 6;
	int positionIterations = 2;
	int timer = 0;
	
	public static void main(String [] args){
		try {
			AppGameContainer app = new AppGameContainer(new Collision("Mario"));
			app.setVSync(true);
			app.start();
		} catch (SlickException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void render(GameContainer arg0, Graphics g) throws SlickException {
		g.draw(player);
		g.draw(sueloInicialDibujado);	
		g.draw(sueloFinalDibujo);
		g.draw(techo);
		
	}

	@Override
	public void init(GameContainer arg0) throws SlickException {
		
		//world creation
		Vec2  gravity = new Vec2(0, 100);
	    boolean doSleep = true;
	    world = new World(gravity,doSleep);
	    
		// Static Body
	    groundBodyDef = new BodyDef();
	    groundBodyDef.position.set(0, 470);
	    groundBodyDef.type = BodyType.STATIC;
	    Body groundBody = world.createBody(groundBodyDef);
	    groundBox = new PolygonShape();
	    groundBox.setAsBox(50,10);
	    groundBody.createFixture(groundBox, 0);
	    
	    //static body 2
	    secondGroundBodyDef = new BodyDef();
	    secondGroundBodyDef.position.set(150, 470);
	    secondGroundBodyDef.type = BodyType.STATIC;
	    Body secondGroundBody = world.createBody(secondGroundBodyDef);
	    secondGroundBody.createFixture(groundBox, 0);
	    
	    //static body 3 (choque arriba)
	    roofBodyDef = new BodyDef();
	    roofBodyDef.position.set(0, 430);
	    roofBodyDef.type = BodyType.STATIC;
	    Body roofBody = world.createBody(roofBodyDef);
	    roofBody.createFixture(groundBox, 0);
	    
	 // Dynamic Body
	    bDPlayer = new BodyDef();
	    bDPlayer.type = BodyType.DYNAMIC;
	    bDPlayer.position.set(0, 460);
	    bPlayer = world.createBody(bDPlayer);
	    dynamicBox = new PolygonShape();
	    dynamicBox.setAsBox(1, 1);
	    fixtureDef = new FixtureDef();
	    fixtureDef.shape = dynamicBox;
	    fixtureDef.density=1;
	    fixtureDef.friction=0.3f;
	    bPlayer.createFixture(fixtureDef);
		
	    // Rectangles
	    player = new Rectangle(0, 0, 15, 10);
	    sueloInicialDibujado = new Rectangle(0, 470, 50, 10);
	    sueloFinalDibujo = new Rectangle(150, 470, 50, 10);
	    techo = new Rectangle(0, 430, 50, 10);
	    
	}

	@Override
	public void update(GameContainer gc, int delta) throws SlickException {
		System.out.println(player.getCenterX());
		Input i = gc.getInput();
		if(i.isKeyDown(Input.KEY_D)){
			world.step(timeStep, velocityIterations, positionIterations);
			player.setX(bPlayer.getPosition().x+1);
			bPlayer.m_linearVelocity.x = 100;
			player.setY(bPlayer.getPosition().y);
		}
		else if(i.isKeyDown(Input.KEY_A)){
			world.step(timeStep, velocityIterations, positionIterations);
			player.setX(bPlayer.getPosition().x-1);
			bPlayer.m_linearVelocity.x = -100;
			player.setY(bPlayer.getPosition().y);
		}
		/*player.setX(player.getX() + 1);
		bDPlayer.position.set(player.getX(), player.getY());*/
		else{
			world.step(timeStep, velocityIterations, positionIterations);
			player.setX(bPlayer.getPosition().x);
			bPlayer.m_linearVelocity.x = 0;
			player.setY(bPlayer.getPosition().y);
			if(!bPlayer.isAwake()){
				timer = 0;
			}
		}
		if(i.isKeyDown(Input.KEY_W) && timer<500){
			timer += delta;
			world.step(timeStep, velocityIterations, positionIterations);
			//bPlayer.applyAngularImpulse(0.4f);
			bPlayer.applyLinearImpulse(new Vec2(0, -30), bPlayer.getWorldCenter());
			//						Body.ApplyImpulse(new b2Vec2(0.0, -3.0), Body.GetWorldCenter());
		}
	
		
	}
}
